﻿namespace LiteBlog.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using LiteBlog.Common;
    using System.Globalization;
    using LiteBlog.Components;
    using LiteBlog.Common.Components;
    using LiteBlog.Common.Entities;
    using LiteBlog.Components.Contracts;
    using System.Web.Script.Serialization;
    using LiteBlog.Components.Enums;
    using LiteBlog.Models;
    using LiteBlog.Controllers.Filters;
    using LiteBlog.Controllers.Contracts;

    public class CommentController : CompController<Comment, string>, ICommentController
    {
        private ICommentComp _commentComponent = null;

        public CommentController(ICommentComp commentComp, ISettingsComp settingsComp, ICacheComp cacheComp)
            : base(commentComp, settingsComp, cacheComp)
        {
            _commentComponent = commentComp;
        }


        [Authorize]
        [AjaxExceptionFilter]
        public ActionResult Approve(string id)
        {
            string[] ids = id.Split(new char[] { ',' }, StringSplitOptions.None);
            foreach (var cid in ids)
                _commentComponent.Approve(cid);
            var result = ids.Length > 1 ? MethodResult.MultipleApproveSuccess : MethodResult.ApproveSuccess;
            var resultModel = GetResultModel(result, string.Empty);
            return Json(resultModel, JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        [AjaxExceptionFilter]
        public ActionResult Reject(string id)
        {
            string[] ids = id.Split(new char[] { ',' }, StringSplitOptions.None);
            foreach (var cid in ids)
                _commentComponent.Reject(cid);
            var result = ids.Length > 1 ? MethodResult.MultipleRejectSuccess : MethodResult.RejectSuccess;
            var resultModel = GetResultModel(result, string.Empty);
            return Json(resultModel, JsonRequestBehavior.AllowGet);
        }

        [ValidateInput(false)]
        [AllowAnonymous]
        public override ActionResult Create(Comment comment)
        {
            if(Request!=null)
                comment.Ip = Request.UserHostAddress;
            comment.Time = DateTimeHelper.Now;
            var result = _component.Create(comment);
            var resultModel = GetResultModel(result, string.Empty);
            var resultModelWithData = new ResultModelWithData<Comment>(resultModel, comment);
            return Json(resultModelWithData, JsonRequestBehavior.AllowGet);
        }

        public override ActionResult Delete(string id)
        {
            string[] ids = id.Split(new char[] {','}, StringSplitOptions.None);
            foreach (var cid in ids)
               _commentComponent.Delete(cid);
            var result = ids.Length > 1 ? MethodResult.MultipleDeleteSuccess : MethodResult.DeleteSuccess;
            var resultModel = GetResultModel(result, string.Empty);
            return Json(resultModel, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public override ActionResult Manage()
        {
            return base.Manage();
        }

        [ValidateInput(false)]
        public override ActionResult Update(string olid, Comment model)
        {
            DateTime time;
            DateTime.TryParseExact(model.FormattedTime, ConfigComp.DateFormat, null, DateTimeStyles.None, out time);
            model.Time = time;
            return base.Update(olid, model);
        }

        public ActionResult Recent()
        {
            var comments = _commentComponent.GetApproved();
            return View("Recent", GetSiteLayout(), comments);
        }

        public override void ResolveComponents()
        {
            
        }
    }
}